package controllers;

import com.bj.util.CodeTools;
import com.bj.util.PageList;
import models.Answer;
import models.Question;
import play.cache.Cache;
import play.modules.router.Any;
import play.mvc.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-8-23
 * Time: 上午12:23
 * To change this template use File | Settings | File Templates.
 */
public class ServiceController extends Controller {


    @Any("/service_1/question/list")
    public static void questionList(int page, int pageSize,boolean noCache) {

        Map map = Cache.get("/service_1/question/list", Map.class);

        if ( map == null || noCache  ) {

            map = new HashMap();
            String sql = " from models.Question order by id desc ";

            if (pageSize <= 0) {
                pageSize = 50;
            }

            long count = Question.count(sql);
            page = PageList.validate(page, pageSize, count);

            List<Question> list = Question.find(sql).fetch((page - 1) * pageSize, pageSize);

            PageList questionList = new PageList(list, page, pageSize, count);
            Map data = new HashMap();
            data.put("pageCount", questionList.getPageCount());
            data.put("totalRowCount", questionList.getTotalRowCount());
            data.put("pageSize", questionList.getPageSize());
            data.put("thisPageSize", questionList.size());


            String ids = "0";
            for (int i = 0; i < list.size(); i++) {
                ids += "," + list.get(i).getId();
            }

            List<Answer> answerList = Answer.find(" from models.Answer where questionId in (" + ids + ")  ").fetch();

            List objectList = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                Map objectMap = CodeTools.toMapByExpose(list.get(i));
                List objectAnswerList = new ArrayList();
                for (int j = 0; j < answerList.size(); j++) {
                    if (objectAnswerList.size() > 4) {
                        break;
                    }
                    if (answerList.get(j).getQuestionId().longValue() == list.get(i).getId().longValue()) {
                        objectAnswerList.add(CodeTools.toMapByExpose(answerList.get(j)));
                    }
                }
                objectMap.put("answerList", objectAnswerList);
                objectList.add(objectMap);
            }

            map.put("list", objectList);
            map.put("page", data);

            Cache.set("/service_1/question/list" , map , "30mn");

        }

        renderJSON(map);

    }

    @Any("/service_1/answer/list")
    public static void answerList(int page, int pageSize) {

        Map map = new HashMap();
        String sql = " from models.Answer ";

        if (pageSize <= 0) {
            pageSize = 50;
        }

        long count = Answer.count(sql);
        page = PageList.validate(page, pageSize, count);

        List list = Answer.find(sql).fetch((page - 1) * pageSize, pageSize);

        PageList questionList = new PageList(list, page, pageSize, count);
        Map data = new HashMap();
        data.put("pageCount", questionList.getPageCount());
        data.put("totalRowCount", questionList.getTotalRowCount());
        data.put("pageSize", questionList.getPageSize());
        data.put("thisPageSize", questionList.size());

        map.put("list", list);
        map.put("page", data);
        renderJSON(map);

    }


}
